package api_sys

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"wrblog-api-go/app/common/token"
	"wrblog-api-go/app/model/model_sys"
	"wrblog-api-go/app/service/service_sys"
	"wrblog-api-go/pkg/request"
	"wrblog-api-go/pkg/result"
	"wrblog-api-go/pkg/status"
)

// @Tags  System - 用户管理
// @Summary  获取用户列表
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param sysUser query model_sys.SelectSysUser true "sysUser"
// @Success 200 {object} result.Result "OK"
// @Router /sysUser/page [get]
func ApiSysUserPageList(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	sysUser := raw.(model_sys.SelectSysUser)
	result.PageRes(service_sys.GetSysUserPageList(sysUser)).Json(c)
}

// @Tags  System - 用户管理
// @Summary  获取用户详情
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param userId path int true "userId"
// @Success 200 {object} result.Result "OK"
// @Router /sysUser/{userId} [get]
func ApiSysUserById(c *gin.Context) {
	userId, err := strconv.Atoi(c.Param("userId"))
	if err != nil {
		result.Wrap(status.BadRequest, "参数读取失败").Json(c)
	} else {
		result.Res(service_sys.GetSysUserById(userId)).Json(c)
	}
}

// @Tags  System - 用户管理
// @Summary  添加用户
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param sysUser body model_sys.SysUser true "sysUser"
// @Success 200 {object} result.Result "OK"
// @Router /sysUser/add [post]
func ApiAddSysUser(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	sysUser := raw.(model_sys.SaveSysUser)
	sysUser.CreateBy = token.GetUk(c)
	result.Res(service_sys.AddSysUser(sysUser)).Json(c)
}

// @Tags  System - 用户管理
// @Summary  修改用户
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param sysUser body model_sys.SysUser true "sysUser"
// @Success 200 {object} result.Result "OK"
// @Router /sysUser/edit [put]
func ApiEditSysUser(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	sysUser := raw.(model_sys.SaveSysUser)
	sysUser.UpdateBy = token.GetUk(c)
	result.Res(service_sys.EditSysUser(sysUser)).Json(c)
}

// @Tags  System - 用户管理
// @Summary  删除用户[逻辑删除]
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param userIds query string true "userIds"
// @Success 200 {object} result.Result "OK"
// @Router /sysUser/removes [delete]
func ApiRemoveSysUser(c *gin.Context) {
	userIds := c.Query("userIds")
	result.Res(service_sys.RemoveSysUser(userIds, token.GetUk(c))).Json(c)
}
